package com.kh.first;

import com.kh.first.pojo.ProductReportPojo;
import com.kh.first.pojo.ReportDetailPojo;
import com.kh.first.pojo.ReportNotesPojo;
import com.kh.first.utils.TN;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.*;

public class GenMapper {
    public static void main(String[] args) throws Exception {
        genMapper(ReportNotesPojo.class);
    }
    private static void genMapper(Class<?> clazz) throws Exception {
        String tableName = clazz.getAnnotation(TN.class).value();
        Map<String, Object> root = new LinkedHashMap<String, Object>();
        String simpleName = clazz.getSimpleName();
        String packageName = "com.kh.first.dao";// clazz.getPackage().toString();
        String beanName = simpleName.replace("Pojo", "");
        String pri = "id";
//        String pri = "userid";
        String daoAllName = packageName + "." + (beanName) + "Dao";
        root.put("daoAllName", daoAllName);
        root.put("packageName", packageName);
        root.put("beanName", beanName);
        root.put("tableName", tableName);
        root.put("beanAllName", clazz.getName());
        root.put("pri", pri);
        Field[] fields = clazz.getDeclaredFields();
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (Field field : fields) {
            String name = field.getName();
            Class<?> type = field.getType();
            Class<?> clazzInt = Integer.class;
            Class<?> clazzLong = Long.class;
            Class<?> clazzDate = Date.class;
            Class<?> clazzDecimal = BigDecimal.class;
//			Class<?> clazzString = String.class;
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("COLUMN_NAME", name);

            map.put("DATA_TYPE", "varchar");
            if (clazzDate == type)
                map.put("DATA_TYPE", "date");
            if (clazzDecimal == type)
                map.put("DATA_TYPE", "decimal");
            if (clazzInt == type || clazzLong == type)
                map.put("DATA_TYPE", "int");
            list.add(map);
        }
        root.put("columns", list);
        root.put("pri_type", "int");
        String fprint = FreemarkerTest.fprint("mapper.ftl", root);
        System.out.println(fprint);
    }
}
